
//
//  SearchViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/7.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "SearchViewController.h"
#import "GiftPickViewController.h"
#import "SearchDetailsViewController.h"
#import "MJRefresh.h"
#import "TAMacro.h"
@interface SearchViewController ()<UISearchBarDelegate>
@property (nonatomic ,strong)UISearchBar * searchBar;
@property (nonatomic ,strong)UIButton * button;
@end
@implementation SearchViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor purpleColor];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
//    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//        [self.tableView.mj_footer beginRefreshing];
//        [self mjRefreshAdd];
//        [self.tableView.mj_footer endRefreshing];
//    }];
    self.navigationItem.leftBarButtonItem =left;
    [self foundImage];
    [self foundSearchBar];
    [self foundRightButton];
    [self UPButton];
    [self foundPickGiftButten];
}
- (void)foundImage {
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    imageView.image = [UIImage imageNamed:@"搜索背景.jpg"];
    [self.view addSubview:imageView];
}
- (void)foundSearchBar {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/10, SCREEN_HEIGHT / 10)];
    [self.searchBar setPlaceholder:@"搜索"];
    self.searchBar.delegate = self;
    self.navigationItem.titleView = self.searchBar;
}
- (void)foundRightButton {
    UIButton *btn_nav_right = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_nav_right.frame = CGRectMake(SCREEN_WIDTH / 8 * 7, 0, SCREEN_WIDTH / 8 , SCREEN_HEIGHT/  10 );
    [btn_nav_right setTitle:@"搜索" forState:UIControlStateNormal];
    [self.view addSubview:btn_nav_right];
    self.searchBar.showsCancelButton = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn_nav_right];
    [btn_nav_right addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickRight:(UIButton *)sender {
    self.hidesBottomBarWhenPushed = YES;
    [_searchBar resignFirstResponder];
    SearchDetailsViewController *searchDetails = [[SearchDetailsViewController alloc]init];
    searchDetails.str = self.seachStr;
    [self.navigationController pushViewController:searchDetails animated:YES];
    self.hidesBottomBarWhenPushed = YES;
}
- (void)UPButton {
    NSArray *array = @[@"手机壳",@"手表",@"情侣",@"双肩包",@"钱包",@"杯子",@"凉鞋",@"连衣裙",@"手链", @"项链", @"手机",@"耳钉"];
    for (int i = 0; i < 12; i++) {
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button.frame = CGRectMake((i % 4 * SCREEN_WIDTH / 5 + SCREEN_WIDTH / 10)  , (i / 4 * SCREEN_HEIGHT / 20 ) + SCREEN_HEIGHT / 8 , SCREEN_WIDTH / 5 , SCREEN_HEIGHT / 20);
        self.button.layer.cornerRadius = 10;
        self.button.layer.borderWidth = 1.0;
        self.button.tag = 1001 + i;
        [self.button setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(chickNum:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.button];
    }
}
- (void)foundPickGiftButten {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, SCREEN_HEIGHT  / 2, SCREEN_WIDTH  , SCREEN_HEIGHT/20);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"选礼神器" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(TOPickGift:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.seachStr = searchText;
}
- (void)TOPickGift:(UIButton *)sender {
    self.hidesBottomBarWhenPushed = YES;
    GiftPickViewController *pickGiftVC = [[GiftPickViewController alloc]init];
    [self.navigationController pushViewController:pickGiftVC animated:YES];
    self.hidesBottomBarWhenPushed = YES;
}
- (void)chickNum:(UIButton *)sender {
    self.hidesBottomBarWhenPushed = YES;
    SearchDetailsViewController *searchDetails = [[SearchDetailsViewController alloc]init];
    searchDetails.str = sender.titleLabel.text;
    [self.navigationController pushViewController:searchDetails animated:YES];
    
    self.hidesBottomBarWhenPushed = YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
    self.hidesBottomBarWhenPushed = YES;
    SearchDetailsViewController *searchDetails = [[SearchDetailsViewController alloc]init];
    searchDetails.str = self.seachStr;
    [self.navigationController pushViewController:searchDetails animated:YES];
    self.hidesBottomBarWhenPushed = YES;
}
- (void)clickLeft:(UIBarButtonItem*)qwe {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
